package ch.uzh.ifi.sonar.importers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import net.sourceforge.clearcase.commandline.CleartoolCommandLine;
import ch.uzh.ifi.sonar.model.ModelFactory;
import ch.uzh.ifi.sonar.model.Stream;
import ch.uzh.ifi.sonar.storage.IStorage;
import ch.uzh.ifi.sonar.storage.StorageResult;
import ch.uzh.ifi.sonar.storage.TeneoHibernateStorage;

public class ClearCaseImporter {

	public static final String VOB_NAME = "UCM_PVOB_PF91";

	// TODO Hard code the integration csv path to decrease the cleartool
	// commandline cost
	/* to run the script in the script folder and then get the csv file */
	public static final String INTEGRATION_CSV_PATH = "D:\\shell_ws\\output\\integration_streams.csv";

	public ClearCaseImporter() {
		storage = new TeneoHibernateStorage();
	}

	public final IStorage storage;

	public ClearCaseImporter(IStorage storage) {
		this.storage = storage;
	}

	public List<String> getStreamsFromCSV() {
		List<String> streams = new ArrayList<String>();
		File csvFile = new File(INTEGRATION_CSV_PATH);
		try {
			BufferedReader rs = new BufferedReader(new FileReader(csvFile));
			String line = "";
			while ((line = rs.readLine()) != null) {
				streams.add(line);
			}
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return streams;
	}

	public List<String> getAllIntegrationUCM() {
		List<String> ucms = new ArrayList<String>();
		List<String> streams = getStreamsFromCSV();
		for (String stream : streams) {
			ucms.addAll(getStreamUCM(stream));
		}
		return ucms;
	}

	public List<String> getStreamUCM(String stream) {
		List<String> ucms = new ArrayList<String>();
		CleartoolCommandLine ct = new CleartoolCommandLine("lsact");
		ct.addOption("-s");
		ct.addElement("-in");
		ct.addElement(stream);

		ClearCaseCommand command = new ClearCaseCommand(ct);
		Map<String, String[]> cmdResult = command.execute();
		String[] output = cmdResult.get(ClearCaseCommand.SUCCEED);
		String[] errorOutput = cmdResult.get(ClearCaseCommand.FAIL);

		if (errorOutput != null && errorOutput.length != 0) {
			System.out
					.println("### Clear Case command executes and returns the following Errors:");
			for (String line : errorOutput) {
				System.out.println(line);
			}
		}

		if (output != null) {
			for (String line : output) {
				ucms.add(line);
			}
		}
		Stream streamEntity = ModelFactory.eINSTANCE.createStream();
		streamEntity.setName(stream);

		return ucms;
	}

	
	
	public List<Stream> getAllStreamFromDB(){
		List<Stream> streams = new ArrayList<Stream>();
		String dbName = storage.getDatabaseName();
		String queryString = "from Stream";
		StorageResult rs = storage.executeListSQLQuery(queryString);
		List<?> streamsList = rs.getListResult();
		for(Object o: streamsList){
			Stream stream = (Stream)o;
			streams.add(stream);
		}
		return streams;		
	}

	private List<String> getAllIntegrationStreams() {
		List<String> streams = new ArrayList<String>();

		CleartoolCommandLine ct = new CleartoolCommandLine("lsstream");
		// ct.addOption("-obs");
		// ct.addOption("-s");
		// ct.addElement("-invob");
		// ct.addElement(VOB_NAME);
		ct.addElement("prjIter5001_Integration");

		ClearCaseCommand command = new ClearCaseCommand(ct);

		// TODO: need to IProgressMonitor and MultiThread
		Map<String, String[]> cmdResult = command.execute();
		String[] output = cmdResult.get(ClearCaseCommand.SUCCEED);
		String[] errorOutput = cmdResult.get(ClearCaseCommand.FAIL);

		if (errorOutput != null && errorOutput.length != 0) {
			System.out
					.println("### Clear Case command executes and returns the following Errors:");
			for (String line : errorOutput) {
				System.out.println(line);
			}
		}

		if (output != null) {
			for (String line : output) {
				if (line.matches("prjIter")) {
					System.out.println(line);
				}
			}
		}

		return streams;
	}
}
